package com.xs;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xs.dao.UserDao;
import com.xs.domain.User;
import com.xs.util.PageInfoDto;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by FlyDonkey on 2019/4/29 22:51
 */
@Service
public class UserService {

    @Resource
    UserDao userDao;

    @Async
    public void testAsyncQuery(){
        List<User> userList = userDao.select(new User());
        System.out.println(userList.toString());
    }

    @Transactional
    public void testTransaction(Boolean needThrowExp) {
        User user =new User();
        user.setId(1);
        user.setAddress("地址【测试事务】");
        userDao.update(user);
        if(needThrowExp){
            throw new RuntimeException("主动抛出异常,此时回滚");
        }
    }

    public PageInfoDto testPage(Integer pageCurrent, Integer pageSize) {
        PageHelper.startPage(pageCurrent,pageSize);
        List<User> userList = userDao.select(new User());
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return PageInfoDto.of(userPageInfo);
    }
}
